/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*************************************************************************
 * File: string.hpp
 *************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_STRING_UTILS_H__
#define __CC_PYTHON_STRING_UTILS_H__

#include "singleton.hpp"
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <python_import_macro.h>

#ifndef _HASH_SEED
#define _HASH_SEED 0xdeadbeef
#endif

NS_CCPY_BEGIN

	class CStringUtils
	{
	public:
		static void split(std::string &s, const char *delim, std::vector<std::string> *ret)
		{
			size_t last = 0;
			size_t index = s.find_first_of(delim, last);
			while (index != std::string::npos)
			{
				ret->push_back(s.substr(last, index - last));
				last = index + 1;
				index = s.find_first_of(delim, last);
			}
			if (index - last > 0)
			{
				ret->push_back(s.substr(last, index - last));
			}
		}

		static char *trim_it(char *str, unsigned int s)
		{
			unsigned int n = 0;
			char *dst = NULL;
			do
			{
				if(*str > 0 && isprint(*str) && !isspace(*str) && *str != '\r' && *str != '\n')
				{
					if(!dst)
					{
						dst = str;
						break;
					}
				}
				str ++;
				n ++;
			}
			while(n < s);

			if(!dst)
				dst = str;

			char *str_end = str + s - 1;
			do
			{
				if(*str_end != 0 && (*str_end < 0 || !isspace(*str_end)) && *str != '\r' && *str != '\n')
				{
					break;
				}
				else
				{
					*str_end = 0;
				}
				str_end --;
			}
			while(str_end >= dst);
			return dst;
		}

		static const char *trim(const char *str, unsigned int s)
		{
			static char tmp[255];
			unsigned int ss = MIN(254, s);
			memcpy(tmp, str, ss);
			tmp[ss] = 0;
			trim_it(tmp, ss);
			return tmp;
		}

		static inline size_t char_hash_value(const char *str)
		{
			if(!str)
				return 0;
			size_t value = _HASH_SEED;
			size_t size = strlen(str);
			if (size > 0)
			{
				size_t temp = (size / 16) + 1;
				size -= temp;
				for (size_t idx = 0; idx <= size; idx += temp)
				{
					value += (size_t)str[(int)idx];
				}
			}
			return value;
		}

		template<typename T>
		static T convert(const char *data)
		{
			T t;
			static std::stringstream ss;
			ss.clear();
			ss.str("");
			ss << data;
			ss >> t;
			return t;
		}
	};

NS_CCPY_END

#endif // !__CC_PYTHON_STRING_UTILS_H__